<?php
namespace app\controller;

use app\BaseController;
use app\model\User;
use think\facade\Db;
use think\facade\Session;
use think\Request;
use app\middleware\Auth;
//use GuzzleHttp\Client;

//use app\middleware\Auth;

class UserController extends BaseController
{
    protected $middleware = [Auth::class];//控制器中间件
    //登录
    public function login(Request $request)
    {
        // $session = Session::get($request->post('username'));
        $authorization=$request->header('authorization');
        // if ($session) {
        //     return success($session);
        // }
        $psd = base64_encode($request->post('password'));
        $where = [
            'username' => $request->post('username'),
            'password' => $psd,
            // 'password' => base64_encode($request->post('password')),
        ];
        $user = User::where($where)->find();

        if ($user) {
            if($user->available==0){
                return fail(null, '已禁止登录');
            }
            $user->last_time = date('Y-m-d H:i:s', time());
            $user->save();
            $hidden =['username','password','password_t','last_time','is_login','create_time','update_time','delete_time'];
            $user->hidden($hidden);
            $token=base64_encode($request->post('username'));
            Session::set($token, $user);
            $user->token=$token;
            return success($user);
        } else {
            return fail(null, '账号或密码错误');
        }
    }
    //添加用户
    public function addUser(Request $request)
    {

        $user = new User;
        $data = $request->post();
        /* 身份添加判断 数组形式 */
        if($request->has('have')){
            $data['have']=implode(',',$data['have']);
        }
        if ($user) {
            foreach ($data as $key => $value) {
                $user->$key = $value;
            }
        }

        $identity = $request->user->identity;
        switch ($identity) {
            case 'admin':
                $user->identity= 'sale';
                break;
            case 'sale':
                $user->identity= 'user';
                $user->belong_id= $request->user->id;
                break;
            default:
                break;
        }
        $user->password = base64_encode($request->post('password_t'));
        $user->password_t = $request->post('password_t');

        $rq = $user->save();
        if ($rq) {
            return success($data);
        }
    }
    public function updateUser(Request $request)
    {
        $user = User::find($request->post('id'));
        $data = $request->post();
        /* 身份添加判断 数组形式 */
        if($request->has('have')){
            $data['have']=implode(',',$data['have']);
        }
        if ($user) {
            foreach ($data as $key => $value) {
                $user->$key = $value;
            }
        }
        $identity = $user->identity;
        switch ($identity) {
            case 'admin':
                $user->identity= 'sale';
                break;
            case 'sale':
                $user->identity= 'user';
                break;
            default:
                break;
        }
        if ($request->post('password_t')) {
            $user->password = base64_encode($request->post('password_t'));
            $user->password_t = $request->post('password_t');
        }
        $rq = $user->save();
        if ($rq) {
            return success($data);
        }
    }
    public function delUser(Request $request)
    {
        $id = $request->post('id');
        $rq = User::destroy($id);
        if ($rq) {
            return success($request);
        }
    }
}
